﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.DO;
using SES.CMS.BL;
using System.Data;
using System.Web.UI.HtmlControls;

namespace SES.CMS
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        public int idProduct = 0;
        public int iQty = 0;
        cmsProductDO itemProductDO = new cmsProductDO();
        public string domain = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            domain = Ultility.TopbikeDomain();
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                if (!IsPostBack)
                {

                    if (Session["shop"] == null)
                    {
                        Session["shop"] = new MyShopCart();
                    }
                    if (Request.QueryString["ID"] == null) return;
                    idProduct = Convert.ToInt32(Request.QueryString["ID"]);
                 //   iQty = Convert.ToInt32(txtQty.Text);
                    LoadImageProduct(idProduct);
                    itemProductDO.ProductID = idProduct;
                    itemProductDO = new cmsProductBL().Select(itemProductDO);
                    LoadData();
                  //  LoadDetail(idProduct);
                    rptProductPropertiesXDataSource(idProduct);
                    ShowDataListProduct();
                    ShowDataListPKDC();
                    FaceComment();
                    LoadSeo();
                    LoadAlbumProduct(idProduct);
                    ltrHuongDan.Text = new sysConfigBL().Select(new sysConfigDO() { ConfigID = 26 }).ConfigValue;
                    //Load color
                    LoadColor(idProduct);
                }
            }
            else
            {

            }
        }
        void LoadColor(int poductID)
        {
            rptColor.DataSource = new ProductColorBL().GetByProduct(poductID);
            rptColor.DataBind();
        }

        void LoadSeo()
        {
            HtmlMeta metaKey = new HtmlMeta();
            metaKey.Name = "keywords";
            metaKey.Content = itemProductDO.Description;
            Page.Header.Controls.Add(metaKey);
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = itemProductDO.Description;
            Page.Header.Controls.Add(metaDesc);

            Page.Title = itemProductDO.ProductTitle + " | " + Ultility.WebTitle();
        }

        void FaceComment()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                string cururl = "http://" + Request.Url.Host + Request.Url.AbsolutePath;
                cmfb.Attributes.Add("data-href", cururl);
            }
        }
        protected void rpListProduct_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
     //       AddItem();
        }
        protected void btnCart_Click(object sender, EventArgs e)
        {
          //  AddItem();
        //    button.Attributes.Add("onlick","updateCart(" + Convert.ToInt32(Request.QueryString["ID"]) + ","  + txtQty.Text + "); return false;");
        }
        cmsProductBL data = new cmsProductBL();
      
        public string imageProduct = "images.jpg";
        public string Thumbnail = "images.jpg";
        void LoadData()
        {
            int idParent = itemProductDO.ProductCategoryID;
            cmsProductCategoryDO itemParent = new cmsProductCategoryDO();
            cmsProductCategoryBL itemParentBL = new cmsProductCategoryBL();
            itemParent.ProductCategoryID = idParent;
            itemParent = itemParentBL.Select(itemParent);

            lblTitleParent.Text = itemParent.ProductCategoryTitle;
            lblTitle.Text = itemProductDO.ProductTitle;
            ltProductTitle.Text = itemProductDO.ProductTitle;
            lbId.Text = itemProductDO.ProductCode;
            lblWarranty.Text = itemProductDO.Warranty;
            if (itemProductDO.ProductImage != null)
                imageProduct = itemProductDO.ProductImage;
            if (!string.IsNullOrEmpty(itemProductDO.Thumbnail))
                Thumbnail = itemProductDO.Thumbnail;
            else
                Thumbnail = imageProduct;
            int Originid = itemProductDO.OriginID;
            cmsOriginDO itemDO = new cmsOriginDO();
            cmsOriginBL itemBL = new cmsOriginBL();
            itemDO.OriginID = Originid;
            itemDO = itemBL.Select(itemDO);
            string xx = itemDO.Origin;
            lbXuatxu.Text = xx;
            string status = string.Empty;
            lbStatus.Text = (itemProductDO.Active) == true ? "Còn hàng" : "Hết hàng";

            if (itemProductDO.IsPromotion)
            {
                if (itemProductDO.SalePrice > 0)
                {
                    lbCost.Text = string.Format("{0:0,0 VNĐ}", itemProductDO.SalePrice);
                }
                else
                {
                    lbCost.Text = "Liên Hệ";
                }
            }
            else
            {
                if (itemProductDO.Price > 0)
                {
                    lbCost.Text = string.Format("{0:0,0 VNĐ}", itemProductDO.Price);
                }
                else
                    lbCost.Text = "Liên Hệ";
            }

            ltrAbout.Text = itemProductDO.DetailDescription;
        }

        cmsProductPropertyDO itemProductPropertyDO = new cmsProductPropertyDO();
        cmsPropertiesDO itemPropertyDO = new cmsPropertiesDO();
        public string name = string.Empty;
        public string nameDetail = string.Empty;
        void LoadDetail(int productID)
        {
            rptProductProperties.DataSource = new cmsProductPropertyBL().SelectByProductID(productID);
            rptProductProperties.DataBind();
        }
        //void LoadDetail()
        //{
        //    DataTable tb = new cmsProductPropertyBL().SelectByProductID(idProduct);
        //    if (tb.Rows.Count > 0)
        //    {
        //        for (int i = 0; i < tb.Rows.Count; i++)
        //        {
        //            DataRow dr = tb.Rows[i];
        //            int idProperty = Convert.ToInt32(dr["PropertyID"].ToString());
        //            itemPropertyDO.PropertyID = idProperty;
        //            itemPropertyDO = new cmsPropertiesBL().Select(itemPropertyDO);
        //            Label lbInfo = new Label();
        //            lbInfo.Text = itemPropertyDO.PropertyName + " : " + itemPropertyDO.PropertyCode + "<br />";
        //            infoDetail.Controls.Add(lbInfo);
        //        }
        //    }
        //    else
        //    {
        //        Label lbInfo = new Label();
        //        lbInfo.Text = "Đang cập nhật...";
        //        infoDetail.Controls.Add(lbInfo);
        //    }
        //}
        void LoadAlbumProduct(int productID)
        {
          //  DataTable dtAlbum = new MediaBL().GetTopProductId(productID, 0);
            //rptMainImage.DataSource = dtAlbum;
            //rptThumbnail.DataSource = dtAlbum;

            //rptMainImage.DataBind();
            //rptThumbnail.DataBind();
        }
        void LoadImageProduct(int productID)
        {
            MediaBL itemBL = new MediaBL();
            DataTable tb = itemBL.GetTopProductId(productID, 2);
            listImage.DataSource = tb;
            listImage.DataBind();
        }
        void ShowDataListProduct()
        {
            int idCateProduct = itemProductDO.ProductCategoryID;
            DataTable tb = new cmsProductBL().SelectByProductCategoryTop(idCateProduct, idProduct);
            if (tb.Rows.Count > 0)
            {
                rpListProduct.DataSource = tb;
                rpListProduct.DataBind();
            }
            //else
            //    other.Visible = false;
        }


        void ShowDataListPKDC()
        {
            int idCateProduct = 12;
            DataTable tb = new cmsProductBL().SelectByProductCategory(idCateProduct);
            if (tb.Rows.Count > 0)
            {
                // rpListProduct.DataSource = tb;
                // rpListProduct.DataBind();
            }
            else { }
            //pkdc.Visible = false;
        }

        protected string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
        protected void rptProductPropertiesXDataSource(int productID)
        {
            rptProductPropertiesX.DataSource = new cmsProductPropertyBL().SelectByProductAndParent(productID, 0);
            rptProductPropertiesX.DataBind();
        }
        protected void rptProductPropertiesX_DataBound(object sender, RepeaterItemEventArgs e)
        {
            int productID = int.Parse(Request.QueryString["ID"]);
            DataRowView drv = (DataRowView)e.Item.DataItem;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                int parentID = int.Parse(drv["PropertyID"].ToString());
                Repeater rptChildProperty = (Repeater)e.Item.FindControl("rptChildProperty");
                rptChildProperty.DataSource = new cmsProductPropertyBL().SelectByProductAndParent(productID, parentID);
                rptChildProperty.DataBind();
            }
        }
        protected void rptChildProperty_DataBound(object sender, RepeaterItemEventArgs e)
        {

            DataRowView drv = (DataRowView)e.Item.DataItem;

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Panel pnlChild = (Panel)e.Item.FindControl("pnlChild");
                if (string.IsNullOrEmpty(drv["Value"].ToString()))
                {
                    pnlChild.Visible = false;
                }
            }
        }
    }
}